<?php

/**
 * CRUD module bootstrap
 *
 * @author     Mon Zafra <monzee at gmail>
 * @copyright  (c)2009 Mon Zafra
 * @category   Mz
 * @package    modules
 * @subpackage crud
 * @license    http://mz-project.googlecode.com/svn/trunk/LICENSE    MIT License
 * @version    SVN: $Id: Bootstrap.php 15 2009-03-04 11:00:01Z monzee $
 */
class Crud_Bootstrap extends Mz_Module_Bootstrap
{

    public function init()
    {
        $vr = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $vr->initView();
        
        $view = $vr->view;
        $view->addHelperPath($this->pathTo('views/helpers'), 'Crud');
    }

    public function getAdapter()
    {
        $default = Zend_Db_Table_Abstract::getDefaultAdapter();
        $iniFile = $this->pathTo('config.ini');
        $session = new Zend_Session_Namespace('crud');

        if ('default' == $session->adapter || !file_exists($iniFile)) {
            return $default;
        }

        if ('custom' == $session->adapter && !empty($session->adapterParams)) {
            $params = $session->adapterParams;
            $adapter = array_shift($params);
        } else {
            $config = new Zend_Config_Ini($iniFile, 'crud');
            $config = $config->db->toArray();

            if (empty($config) || !empty($config['useDefault'])) {
                return $default;
            }

            $adapter = $config['adapter'];
            $params = $config['params'];
        }

        return Zend_Db::factory($adapter, $params);
    }


}